package com.gancp.service.impl;


import com.gancp.dao.IBaseMapper;
import com.gancp.dao.user.AttendMapper;
import com.gancp.domain.Attend;
import com.gancp.service.AttendBiz;
import com.gancp.service.base.BaseBizImpl;
import com.gancp.utills.DateUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.sql.SQLData;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by GCP on 2018/5/1.
 * 打卡业务实现类
 */
@Service("AttendBiz")
@Transactional
public class AttendBizImpl extends BaseBizImpl<Attend, Long> implements AttendBiz {
    private Logger log = LoggerFactory.getLogger( AttendBizImpl.class );
    @Autowired
    private AttendMapper attendMapper;

    @Override
    protected IBaseMapper getIBaseMapper() {
        return attendMapper;
    }


    // 一整天的缺勤时间
    private static final Integer ABSENCE_DAY = 480;
    // 正常
    private static final Byte ATTEND_STATUS_NORMAL = 1;
    //  异常
    private static final Byte ATTEND_STATUS_ABNORMAL = 2;

    @Override
    public String signAttend(Attend attend) throws Exception {
        String msg = "";
        try {
            // 以中午十二点为界限区分上下午打卡
            // 下午打卡的时候要计算当天的打卡信息
            Date today = new Date();
            // 查询今天的打卡记录
            Date sqlDate = new java.sql.Date( today.getTime() );
            Attend todatRecord = attendMapper.selectSignRecord( sqlDate, attend.getUserid() );
            attend.setAttendDate( today );
            Date noon = DateUtils.getDate( DateUtils.NOON_HOUR, DateUtils.NOON_MINUTE );
            if (today.compareTo( noon ) <= 0) {
                msg = signMoring( todatRecord, attend );
            } else {
                msg = signEvening( todatRecord, attend );
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return msg;
    }

    @Override
    public List<Attend> selectByUserid(Long userid) throws Exception {

        try {
            List<Attend> attendList = attendMapper.selectByUserid( 5L );
            return attendList;
        } catch (Exception e) {
            throw e;
        }

    }

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 根据查询条件分页查询
     **/
    @Override
    public List<Attend> selectAttendsPage(Map<String, Object> param) throws Exception {

        List<Attend> result = attendMapper.selectAttendListByPage( param );
        return result;
    }


    private String signMoring(Attend todatRecord, Attend insertAttend) {
        String msg = "";
        // 今天早上没有打卡
        if (todatRecord == null) {
            insertAttend.setAttendDate( new Date() );
            insertAttend.setAttendMorning( new Date() );
            insertAttend.setAttendWeek( (byte) DateUtils.getTodayWeek() );
            attendMapper.insertSelective( insertAttend );
            msg = "您上午打卡成功！";
        } else {
            msg = "您上午已经打过卡啦！";
        }
        return msg;
    }

    private String signEvening(Attend todatRecord, Attend insertAttend) {
        if (todatRecord == null) {
            insertAttend.setAttendEvening( new Date() );
            insertAttend.setAttendDate( new Date() );
            insertAttend.setAttendWeek( (byte) DateUtils.getTodayWeek() );
            attendMapper.insertSelective( insertAttend );
        } else {
            todatRecord.setAttendEvening( new Date() );
            attendMapper.updateByPrimaryKeySelective( todatRecord );
        }
        return "您下午打卡成功！";
    }


    @Override
    public void checkAttendTask() throws Exception {
        //  先查询昨日的所有打卡记录
        //List<Attend> userIdList = attendMapper.selectYestarDayRecond();
        //查询缺勤用户ID 插入打卡记录  并且设置为异常 缺勤480分钟
        List<Long> userId = attendMapper.selectYestarDayAbsence();
        Attend attend = null;
        List<Attend> attendList = new ArrayList<Attend>();
        Date yestaryDay = DateUtils.getYestaryDay();
        byte week = (byte) (DateUtils.getTodayWeek() - 1 <= 0 ? 7 : DateUtils.getTodayWeek() - 1);
        for (Long id : userId) {
            attend = new Attend();
            attend.setUserid( id );
            attend.setAttendDate( yestaryDay );
            attend.setAttendWeek( week );
            attend.setAbsence( ABSENCE_DAY );
            attend.setAttendStatus( ATTEND_STATUS_ABNORMAL );
            attendList.add( attend );
        }
        attendMapper.batchInsert( attendList );
    }


}
